package in.muton.edushare.panels.upload.single;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.panels.upload.Uploader;
import in.muton.edushare.panels.upload.exceptions.FileProcessingException;
import in.muton.edushare.panels.upload.exceptions.InvalidUploadFileException;
import in.muton.wicket.extensions.upload.UploadProcessProgressBar;
import in.muton.wicket.extensions.upload.UploadProcessWebRequest;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.wicket.RequestCycle;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.upload.FileUpload;
import org.apache.wicket.markup.html.form.upload.FileUploadField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.util.lang.Bytes;

import com.edushare.common.constants.ConfigPropertyName;
import com.edushare.common.global.SystemGlobals;
import com.edushare.common.vo.FileDirectories;
import com.edushare.common.vo.FilesUploaded;

public class SingleUploadPanel extends EdusharePanel {

	private static final long serialVersionUID = -1423915375033035574L;

	public SingleUploadPanel(String id, IModel<FileDirectories> model) {
		super(id, model);
		final FileUploadField uField = new FileUploadField("uField");
		Form<Void> uForm = new Form<Void>("uForm"){

			private static final long serialVersionUID = -2386990011563674160L;

			@Override
			protected void onSubmit() {
				FileUpload fileUpload = uField.getFileUpload();
				Uploader uploader = new Uploader(
										fileUpload, 
										(FileDirectories)SingleUploadPanel.this.getDefaultModelObject());
				
				FilesUploaded uploadedFile = null;
				try {
					uploadedFile = uploader.startUploading();
				} catch (InvalidUploadFileException e) {
					warn("Invalid File.");
					e.printStackTrace();
				} catch (IOException e) {
					warn("Error during upload");
					e.printStackTrace();
				} catch (FileProcessingException e) {
					warn("Error in processing this file");
					e.printStackTrace();
				}
				
				EduShareSession.get().setFileUploadedInLastUpload(uploadedFile);
				postprocessingCleanup();
				afterUploadFinished(uploadedFile);
			}
		};
		
		uForm.setMultiPart(true);
		uForm.setMaxSize(Bytes.kilobytes(
				Long.parseLong(
						SystemGlobals.getInstance()
						.getConfigProperty(ConfigPropertyName.UPLOAD_MAXSIZE_KB)
						)
						));
		uForm.add(uField);
		uForm.add(new UploadProcessProgressBar("progressBar",uForm));
		add(uForm);
	}
	
	private void postprocessingCleanup(){
		HttpServletRequest req = ((WebRequest)RequestCycle.get().getRequest()).getHttpServletRequest();
		UploadProcessWebRequest.clearUploadInfo(req);
	}
	
	/**
	 * <p>
	 * Override this method for any post-processing you may require after the upload is finished.
	 * </p>
	 * <p>
	 * Please note that if upload is taking place in some other request thread (like from an Modal Window), 
	 * you may not access the file uploaded. In that case, use session.
	 * </p>
	 * <pre>EduShareSession.get().getFileUploadedInLastUpload(uploadedFile);</pre>
	 * 
	 * @param uploadedFile
	 */
	public void afterUploadFinished(FilesUploaded uploadedFile) {}

}
